package com.sls.api.game.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.sls.api.game.constant.Constant;
import com.sls.api.game.pojo.SnnsInfo;
import com.sls.api.util.IpUtil;
import com.sls.api.util.PropertiesUtil;
import com.sls.api.util.UtilMD5Encryption;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;

public class SevenNineNineSixOrderServlet extends SuperServlet {

	private static final long serialVersionUID = -7329387139029481123L;
	private static Logger log = Logger
			.getLogger(SevenNineNineSixOrderServlet.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setAttribute("org.eclipse.jetty.server.Request.queryEncoding",
				"GB2312");
		resp.setContentType("text/html;charset=GB2312");
		resp.setStatus(HttpServletResponse.SC_OK);
		log.info("7996异步通知的参数：" + req.getQueryString());
		String sid = req.getParameter("sid");
		String ste = req.getParameter("ste");
		String cid = req.getParameter("cid");
		String pid = req.getParameter("pid");
		String oid = req.getParameter("oid");
		String pn = req.getParameter("pn");
		String fm = req.getParameter("fm");
		String sign = req.getParameter("sign");
		boolean flag = this.checkData(sid, ste, cid, pid, oid, pn, fm, sign,
				resp);
		if (flag) {
			try {
				SocketDTO dto = this.sendMsg(sid, ste, cid, pid, oid, pn, fm,
						sign, req);
				Head head = dto.getHead();
				String resCode = head.getResponseCode();
				if (Constant.SUCCESS.equals(resCode)) {
					resp.getWriter().write("success");
					close(resp.getWriter());
				} else {
					resp.getWriter().write("未知错误!");
					close(resp.getWriter());
				}
			} catch (Exception e) {
				resp.getWriter().write("未知错误!");
				close(resp.getWriter());
			}
		}

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}

	private SocketDTO sendMsg(String sid, String ste, String cid, String pid,
			String oid, String pn, String fm, String sign,
			HttpServletRequest req) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("");
		head.setIp(IpUtil.getIpAddr(req));
		head.setUserId("0");
		SnnsInfo sInfo = new SnnsInfo();
		sInfo.setSid(sid);
		sInfo.setSte(ste);
		sInfo.setCid(cid);
		sInfo.setPid(pid);
		sInfo.setOid(oid);
		sInfo.setPn(pn);
		sInfo.setFm(fm);
		sInfo.setSign(sign);
		return super.send(head, sInfo);
	}

	private boolean checkData(String sid, String ste, String cid, String pid,
			String oid, String pn, String fm, String sign,
			HttpServletResponse resp) throws IOException {
		if (StringUtils.isEmpty(sid)) {
			resp.getWriter().write("7996系统生成的订单号为空");
			close(resp.getWriter());
			return false;
		} else if (StringUtils.isEmpty(ste)) {
			resp.getWriter().write("7996订单状态为空");
			close(resp.getWriter());
			return false;
		} else if (StringUtils.isEmpty(cid)) {
			resp.getWriter().write("商户在7996系统注册的ID为空");
			close(resp.getWriter());
			return false;
		} else if (StringUtils.isEmpty(pid)) {
			resp.getWriter().write("7996商户编号为空");
			close(resp.getWriter());
			return false;
		} else if (StringUtils.isEmpty(oid)) {
			resp.getWriter().write("商户订单号为空");
			close(resp.getWriter());
			return false;
		} else if (StringUtils.isEmpty(pn)) {
			resp.getWriter().write("充值帐号为空");
			close(resp.getWriter());
			return false;
		} else if (StringUtils.isEmpty(fm)) {
			resp.getWriter().write("充值金额为空");
			close(resp.getWriter());
			return false;
		} else if (StringUtils.isEmpty(sign)) {
			resp.getWriter().write("MD5签名为空");
			close(resp.getWriter());
			return false;
		} else {
			String md5key = PropertiesUtil.getValue("snns.md5.key");
			String data = sid + ste + cid + pid + oid + pn + fm + md5key;
			String mySign = UtilMD5Encryption.createMD5StringOfGB2312(data);
			if (sign.equals(mySign)) {
				return true;
			} else {
				resp.getWriter().write("MD5签名失败");
				close(resp.getWriter());
				return false;
			}
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param pw
	 */
	private void close(PrintWriter pw) {
		if (pw != null) {
			pw.flush();
			pw.close();
		}
	}
}
